<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item :label="$t('dict.dictName')" prop="dictType">
        <el-tag>{{dataForm.dictTypeName}}-- {{dataForm.dictType}}</el-tag>
      </el-form-item>
      <el-form-item :label="$t('dict.label')" prop="dictLabel">
        <el-input v-model="dataForm.dictLabel" :placeholder="$t('dict.dictName')" maxlength="100"></el-input>
      </el-form-item>
      <el-form-item :label="$t('dict.dictValue')" prop="dictValue">
        <el-input v-model="dataForm.dictValue" :placeholder="$t('dict.dictValue')" maxlength="100"></el-input>
      </el-form-item>
      <el-form-item :label="$t('dict.sort')" prop="sort">
        <el-input-number v-model="dataForm.sort" :min="0" :max="10"></el-input-number>
      </el-form-item>

      <el-form-item :label="$t('dict.remark')" prop="remark">
        <el-input v-model="dataForm.remark" :placeholder="$t('dict.remark')" type="textarea" :autosize="{ minRows: 2, maxRows: 4}"></el-input>
      </el-form-item>
      <el-form-item prop="status" :label="$t('dict.state')" size="mini">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">{{ $t('dict.status1') }}</el-radio>
          <el-radio :label="0">{{ $t('dict.status0') }}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'

export default {
  data () {
    return {
      visible: false,
      loading: false,
      dataForm: {
        id: '',
        pid: '',
        pids: '',
        sort: 0,
        isLeaf: 0,
        level: 0,
        pnames: '',
        dictLabel: '',
        dictValue: '',
        dictType: '',
        dictTypeName: '',
        remark: '',
        cssStyle: '',
        cssClass: '',
        status: 1,
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
        version: ''
      },
      dictType: {}
    }
  },
  computed: {
    dataRule () {
      return {
        dictLabel: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        dictValue: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
        ],
        dictType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
        ],
        remark: [
          { max: 200, message: '长度不能超过 200 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        } else {
          this.getDictDataSort()
        }
      })
    },
    // 根据pid 获取最大sort
    getDictDataSort () {
      this.$http.get(`/sys/sysdictdata/getDictDataMaxSort/${this.dataForm.pid}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.dataForm.sort = res.data.sort
      }).catch(() => {
      })
    },
    // 获取信息
    getInfo () {
      this.loading = true
      this.$http.get(`/sys/sysdictdata/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/sys/sysdictdata/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {
        })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
